package GameLogic

import (
	"PBMessage"
	"time"

	"git.oschina.net/yangdao/extlib/math3d"

	"git.oschina.net/yangdao/extlib/log"
	"github.com/golang/protobuf/proto"
)

type hitFlyState struct {
	*genericState
	addTime int
}

func (this *hitFlyState) onEnter(args ...interface{}) {
	hitDir := args[0].(math3d.Vec3)
	this.addTime = 0
	state := &PBMessage.SC_UnitState{
		UnitId: proto.Int64(this.gameUnit.GUID),
		State:  proto.Int32(5),
		Time:   proto.Int64(time.Now().UnixNano() / 1e6),
		IntP0:  proto.Int32(int32(this.gameUnit.Pos().X * 100)),
		IntP1:  proto.Int32(int32(this.gameUnit.Pos().Z * 100)),
		IntP2:  proto.Int32(int32(hitDir.X * 100)),
		IntP3:  proto.Int32(int32(hitDir.Z * 100)),
	}
	this.gameUnit.Scene.(*GameScene).sendEachPlayer(PBMessage.PBID_SC_UnitState, state)
	this.gameUnit.Base().IsInvincible = true

	this.gameUnit.hitMover.MoveDistance = 3
	this.gameUnit.hitMover.Speed = 15
	this.gameUnit.hitMover.MoverDir = &hitDir

	this.gameUnit.hitMover.Start(this.gameUnit.Unit, nil)
	log.Log("GameLogic,Info", "单位进入击飞状态")
}

func (this *hitFlyState) onUpdate(dt int) {
	this.addTime += dt
	if this.addTime > 1500 {
		if this.gameUnit.Base().IsDie == false {
			this.gameUnit.chageState("idle")
		}
		this.gameUnit.Base().IsInvincible = false
		this.addTime = 0
	}
}
